﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HalconDotNet;
using HalconExtension.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;
using WpfApp1.Utils;

namespace WpfApp1.ViewModels
{
    public class HalconViewModel : ObservableObject
    {

        public RelayCommand ReadImgBtn { get; set; }

        public RelayCommand LocateBtn { get; set; }

        public RelayCommand InitRectangleBtn { get; set; }

        public RelayCommand InitImgBtn { get; set; }

        public RelayCommand InitCircleBtn { get; set; }

        #region 绘制图案

        public RelayCommand DrawRectangleBtn { get; set; }

        public RelayCommand DrawCircleBtn { get; set; }

        public RelayCommand DrawArrowBtn { get; set; }

        public RelayCommand DrawTextBtn { get; set; }

        public RelayCommand DrawCrossBtn { get; set; }

        public RelayCommand DrawCleanBtn { get; set; }

        private HDrawingObject drawRectangleObj = new HDrawingObject();

        private HDrawingObject drawCircleObj = new HDrawingObject();

        private HDrawingObject drawArrowObj = new HDrawingObject();

        private HDrawingObject drawTextObj = new HDrawingObject();

        private HDrawingObject drawCrossObj = new HDrawingObject();




        #endregion

        #region 鼠标绘制

        public RelayCommand DrawRectByMouseBtn { get; set; }

        public RelayCommand DrawCircleByMouseBtn { get; set; }

        private HRectangleObj HRectangleObjByMouse;

        private HCircleObj HCircleObjByMouse;

        #endregion

        /// <summary>
        /// Halcon组件加载
        /// </summary>
        private HSmartWindowControlWPF halconWindow;
        public HSmartWindowControlWPF HalconWindow
        {
            get => halconWindow;
            set
            {
                value.MouseRightButtonDown += (sender, e) => HalconAPI.CancelDraw();

                halconWindow = value;
                MsgHelper.InfoGlobal("Halcon WPF 组件加载成功！默认添加右键释放功能");


            }
        }

        private HImage hImage { get; set; } = new HImage();

        private HDrawingObject rect = new HDrawingObject();

        private HDrawingObject circle = new HDrawingObject();

        private HDrawingObject.HDrawingObjectCallback objectCallback { get; set; }
        /// <summary>
        /// 模板匹配文件
        /// </summary>
        private HTuple template_model = new HTuple();

        public HRectangleObj RectangleObj { get; set; }

        public HCircleObj CircleObj { get; set; }



        public HalconViewModel()
        {
            ReadImgBtn = new RelayCommand(() =>
            {
                try
                {
                    MsgHelper.Info("读取图片");
                    hImage.ReadImage("Resources/1.png");

                    HalconWindow.HalconWindow.DispObj(hImage);

                    //读取后将图片居中
                    HalconWindow.SetFullImagePart();
                    //读取后将图片完全拉伸充满整个屏幕
                    //HalconWindow.SetFullImagePart(Image);

                    //将笔刷颜色设置为红色
                    HOperatorSet.SetColor(HalconWindow.HalconWindow, "red");
                    //将图像设置为无填充
                    HOperatorSet.SetDraw(HalconWindow.HalconWindow, "margin");



                }
                catch (Exception ex)
                {

                    MsgHelper.Error("HS组件加载图片失败!:" + ex.Message);
                }

            });

            LocateBtn = new RelayCommand(() =>
            {
                HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
                HTuple hv_Angle = new HTuple(), hv_Score = new HTuple();
                HOperatorSet.ReadShapeModel("Resources/output.shm", out template_model);
                MsgHelper.Info("模板匹配文件加载成功！");
                HOperatorSet.FindShapeModel(hImage, template_model, -0.39, 0.79, 0.5, 1, 0.5, "least_squares", 0, 0.9, out hv_Row, out hv_Column, out hv_Angle, out hv_Score);

                //输出匹配结果
                MsgHelper.Success($"分数:{hv_Score},Row坐标:{hv_Row},Col坐标:{hv_Column},角度:{hv_Angle}");

                //将笔刷颜色设置为红色
                HOperatorSet.SetColor(HalconWindow.HalconWindow, "red");

                //在HS控件上面绘制十字标
                HOperatorSet.DispCross(HalconWindow.HalconWindow, hv_Row, hv_Column, 30, hv_Angle);
            });

            InitRectangleBtn = new RelayCommand(() =>
            {
                MsgHelper.Info("生成矩形");
                //rect = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE1, 100, 200, 300, 400);


                //// 显示绘制图案
                //HalconWindow.HalconWindow.AttachDrawingObjectToWindow(rect);
                //// 去除绘制图案
                ////HalconWindow.HalconWindow.DetachDrawingObjectFromWindow(rect);

                //rect.OnDrag(new HDrawingObject.HDrawingObjectCallbackClass((id, window, type) =>
                //{
                //    var hv_type = id.GetDrawingObjectParams("type");
                //    var row1 = id.GetDrawingObjectParams("row1");
                //    var row2 = id.GetDrawingObjectParams("row2");
                //    var column1 = id.GetDrawingObjectParams("column1");
                //    var column2 = id.GetDrawingObjectParams("column2");
                //    //打印坐标
                //    NLogHelper.Debug($"拖动事件: {hv_type}:{row1} {column1} {row2} {column2} ");
                //}));
                //初始化对象
                RectangleObj = new HRectangleObj(HalconWindow, hImage);
                //刷新回调里面进行打印
                RectangleObj.RefleshCallBack = (obj) =>
                {
                    NLogHelper.Debug(obj.ToString());
                };
                //显示对象
                RectangleObj.Show();
            });

            InitCircleBtn = new RelayCommand(() =>
            {
                MsgHelper.Info("生成圆形");
                CircleObj = new HCircleObj(HalconWindow, hImage);
                CircleObj.RefleshCallBack = (obj) =>
                {
                    NLogHelper.Debug(obj.ToString());

                };
                //显示对象
                CircleObj.Show();
            });

            InitImgBtn = new RelayCommand(() => InitTemplate());

            DrawArrowBtn = new RelayCommand(() => DrawArrow());

            DrawCircleBtn = new RelayCommand(() => DrawCircle());

            DrawRectangleBtn = new RelayCommand(() => DrawRectangle());

            DrawTextBtn = new RelayCommand(() => DrawText());

            DrawCrossBtn = new RelayCommand(() => DrawCross());

            HRectangleObjByMouse = new HRectangleObj(HalconWindow, hImage);

            HCircleObjByMouse = new HCircleObj(HalconWindow, hImage);


            DrawRectByMouseBtn = new RelayCommand(() => DrawRectByMouse());

            DrawCircleByMouseBtn = new RelayCommand(() => DrawCircleByMouse());
        }

        /// <summary>
        /// 生成匹配模板
        /// </summary>
        private void InitTemplate()
        {
            HTuple modelId = new HTuple();
            var ho_ImageReduced = RectangleObj.Reduce();
            HOperatorSet.CreateShapeModel(ho_ImageReduced, "auto", -0.39, 0.79, "auto", "auto",
    "use_polarity", "auto", "auto", out modelId);
            //导出匹配模板
            HOperatorSet.WriteShapeModel(modelId, RectangleObj.OutputPath + "\\output.shm");
            MsgHelper.Success($"生成模板成功！路径为:{RectangleObj.OutputPath + "\\output.shm"}");

        }
        private void DrawRectangle()
        {

            HOperatorSet.DispRectangle1(HalconWindow.HalconWindow, 100, 200, 300, 400);
        }

        private void DrawArrow()
        {

            HOperatorSet.DispArrow(HalconWindow.HalconWindow, 20, 20, 130, 130, 10);
        }

        private void DrawCircle()
        {
            HOperatorSet.DispCircle(HalconWindow.HalconWindow, 64, 64, 64);

        }

        private void DrawText()
        {

            HOperatorSet.DispText(HalconWindow.HalconWindow, "打印消息", "window", 12, 12, "black", null, null);

        }

        private void DrawCross()
        {
            HOperatorSet.DispCross(HalconWindow.HalconWindow, 20, 20, 30, 0);

        }


        private void DrawRectByMouse()
        {
            HRectangleObjByMouse = new HRectangleObj(HalconWindow, hImage);

            MsgHelper.Info("请添加鼠标绘制任务！");

            //新建异步，防止阻塞UI
            Task.Run(() => HRectangleObjByMouse.Draw(() =>
            {
                //打印绘制结果
                MsgHelper.Success("鼠标绘图绘制成功！" + HRectangleObjByMouse.ToString());
            }));
        }

        private void DrawCircleByMouse()
        {
            HCircleObjByMouse = new HCircleObj(HalconWindow, hImage);

            MsgHelper.Info("请添加鼠标绘制任务！");

            //新建异步，防止阻塞UI
            Task.Run(() => HCircleObjByMouse.Draw(() =>
            {
                //打印绘制结果
                MsgHelper.Success("鼠标绘图绘制成功！" + HCircleObjByMouse.ToString());
            }));
        }


    }
}
